import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15

ApplicationWindow {
    width: 640
    height: 480
    visible: true
    title: qsTr("自定义消息框示例")

    Column {
        anchors.centerIn: parent
        spacing: 20

        Button {
            text: "信息提示"
            onClicked: {
                messageBox.show("操作完成!", "信息", messageBox.info);
            }
        }

        Button {
            text: "警告提示"
            onClicked: {
                messageBox.showWarning("磁盘空间不足！", "警告", messageBox.showWarning);
            }
        }

        Button {
            text: "错误提示"
            onClicked: {
                messageBox.showError("文件打开失败！", "错误", messageBox.error);
            }
        }

        Button {
            text: "询问提示"
            onClicked: {
                messageBox.showQuestion("确定要删除这个文件吗？", "确认删除", messageBox.question);
            }
        }

        Button {
            text: "自定义图标消息"
            onClicked: {
                customMessage.open();
            }
        }

    }

    Dialog {
        id: messageBox

        property int messageType: 0
        // 消息类型枚举
        readonly property int info: 0
        readonly property int warning: 1
        readonly property int error: 2
        readonly property int question: 3

        function show(message, title, type) {
            messageBox.title = title;
            messageText.text = message;
            messageBox.messageType = type;
            setIcon(type);
            messageBox.open();
        }

        // 修复：将方法移到Dialog内部
        function showWarning(message, title, type) {
            show(message, title, warning);
        }

        function showError(message, title, type) {
            show(message, title, error);
        }

        function showQuestion(message, title, type) {
            show(message, title, question);
        }

        function setIcon(type) {
            switch (type) {
            case messageBox.info:
                iconText.text = "💡"
                standardButtons = Dialog.Ok
                break
            case messageBox.warning:
                iconText.text = "⚠️"
                standardButtons = Dialog.Ok
                break
            case messageBox.error:
                iconText.text = "❌"
                standardButtons = Dialog.Ok 

                break
            case messageBox.question:
                iconText.text = "❓"
                standardButtons = Dialog.Ok | Dialog.Cancel
                break
            }
        }

        // modal: true
        // standardButtons: Dialog.Ok
        // x: (Window.width - width) / 2
        // y: (Window.height - height) / 2
        // width: 300
        onAccepted: {
            // 处理确认按钮点击事件
            console.log("用户点击了确认按钮");
        }
        onRejected: {
            console.log("用户点击了取消按钮");
        }
        x: (window.width - width) / 2
        y: (window.height - height) / 2
        width: 350

        ColumnLayout {
            width: parent.width
            spacing: 15

            RowLayout {
                Label {
                    id: iconText

                    font.pixelSize: 24
                    Layout.alignment: Qt.AlignTop
                }

                Label {
                    id: messageText

                    wrapMode: Text.WordWrap
                    Layout.fillWidth: true
                }

            }

        }

    }

    Dialog {
        // property alias progressText: progressBar.text

        id: progressDialog

        property alias progressValue: progressBar.value

        title: "处理中..."
        modal: true
        standardButtons: Dialog.Cancel
        x: (Window.width - width) / 2
        y: (Window.height - height) / 2
        width: 400
        onRejected: {
            console.log("用户取消了操作");
        }

        ColumnLayout {
            width: parent.width
            spacing: 15

            ProgressBar {
                // text: progressValue + "%"

                id: progressBar

                from: 0
                to: 100
                value: 0
                Layout.fillWidth: true

                Text {
                    anchors.centerIn: parent
                    text: Math.round(parent.value) + "%"
                }

            }

        }

    }

    Dialog {
        id: customMessage

        title: "自定义消息"
        modal: true
        standardButtons: Dialog.Ok

        ColumnLayout {
            width: parent.width
            spacing: 15

            RowLayout {
                Label {
                    text: "🎨"
                    font.pixelSize: 24
                    Layout.alignment: Qt.AlignTop
                }

                Label {
                    text: "这是一个自定义图标的消息框"
                    wrapMode: Text.WordWrap
                    Layout.fillWidth: true
                }

            }

        }

    }

    Timer {
        id: progressTimer

        property int progress: 0

        interval: 100
        repeat: true
        onTriggered: {
            progress += 2;
            progressDialog.progressValue = progress;
            if (progress >= 100) {
                stop();
                progress = 0;
                progressDialog.close();
                messageBox.show("操作完成！", "提示", messageBox.info);
            }
        }
    }

    Button {
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        text: "启动进度条对话框"
        onClicked: {
            progressDialog.open();
            progressTimer.start();
        }
    }

}
